﻿using System;
using System.Collections.Generic;
using System.Text;
using SoccerEvaluator.Common;

namespace SoccerEvaluator.Business
{
    public class Util
    {
        /// <summary>
        /// Lista los nombres diferentes de la lista de scores
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<string> GetDistinct(List<TemplateScoreInfo> list)
        {
            List<string> col = new List<string>();

            foreach (TemplateScoreInfo s in list)
            {
                bool exist = col.Exists(delegate(string name) { return name == s.Local; });

                if (!exist)
                {
                    col.Add(s.Local);
                }


                exist = col.Exists(delegate(string name) { return name == s.Visitor; });

                if (!exist)
                {
                    col.Add(s.Visitor);
                }
            }

            col.Sort();

            return col;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<string> GetDistinctLocations(List<TemplateScoreInfo> list)
        {
            List<string> col = new List<string>();

            foreach (TemplateScoreInfo s in list)
            {
                bool exist = col.Exists(delegate(string name) { return name == s.Location; });

                if (!exist)
                {
                    col.Add(s.Location);
                }

            }

            col.Sort();

            return col;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="f"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<TemplateScoreInfo> Filter(FilterInfo f, List<TemplateScoreInfo> list)
        {
            for (int i = list.Count - 1; i >= 0; i--)
            {
                TemplateScoreInfo s = list[i];

                #region Filtro por fecha
                if (f.Date.HasValue)
                {
                    bool assertDate = (f.Date.Value.Year == s.Date.Year && f.Date.Value.DayOfYear == s.Date.DayOfYear);

                    if (!assertDate)
                    {
                        list.RemoveAt(i);

                        continue;
                    }
                }
                #endregion               

                #region Filtro por ubicacion
                if (f.Location != null)
                {
                    if (f.Location != s.Location)
                    {
                        list.RemoveAt(i);

                        continue;
                    }
                }
                #endregion

                #region Filtro por Local y Visitante
                if (f.Local != null && f.Visitor != null)
                {
                    if (f.Local != s.Local && f.Visitor != s.Visitor)
                    {
                        list.RemoveAt(i);

                        continue;
                    }
                }
                #endregion
            }

            return list;
        }
    }
}
